package com.sgb.kjwl.view.ui.activity.manage.special;


import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.callback.OnTabSelectedListener;
import com.sgb.kjwl.databinding.ActivitySpecialmenberNewBinding;
import com.sgb.kjwl.view.event.LogisticsEvent;
import com.sgb.kjwl.view.event.self.AgentApplyEvent;
import com.sgb.kjwl.view.ui.fragment.manage.special.SpecialMenberNewFragment;
import com.sgb.kjwl.view.ui.fragment.transport.OwnerAndDriverIFragment;
import com.sgb.kjwl.viewmodel.manage.SpecialMenBerNewModel;
import com.swgk.core.event.EventConstant;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

public class SpecialMenBerNewActivity  extends AppActivity {
    ActivitySpecialmenberNewBinding binding;
    SpecialMenBerNewModel viewModel;
    private String[] Titles = {"全部","待受理","已通过", "已拒绝"};
    private ArrayList<Fragment> mFragments=new ArrayList<>();
    public static void start(Context context){
        Intent intent =new Intent(context,SpecialMenBerNewActivity.class);
        context.startActivity(intent);

    }
    @Override
    protected void initView() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        binding = DataBindingUtil.setContentView(SpecialMenBerNewActivity.this, R.layout.activity_specialmenber_new);
        viewModel =new ViewModelProvider(this).get(SpecialMenBerNewModel.class);
        binding.include.titleView.setText("员工审核");
        binding.include.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setview();
    }
   //初始化tab
    public void setview(){
        for (int i=0;i<Titles.length;i++){
            mFragments.add(SpecialMenberNewFragment.newInstance(i));
        }
        SlidingFragmentPagerAdapter pagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments,Titles);
        binding.viewPage.setOffscreenPageLimit(Titles.length);
        binding.viewPage.setAdapter(pagerAdapter);
        binding.tablayout.setupWithViewPager(binding.viewPage);
        binding.tablayout.addOnTabSelectedListener(new OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                refreshData();
            }
        });
    }
    public void refreshData() {
        ((SpecialMenberNewFragment) mFragments.get(binding.tablayout.getSelectedTabPosition())).refreshData();

    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(AgentApplyEvent event) {
        if ( event.getMessageTag() == EventConstant.EVENT_AGENT_APPLY_REJECT || event.getMessageTag() == EventConstant.EVENT_AGENT_APPLY_SUCCESS) {
            refreshData();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }
}
